/*
 * Copyright 2006 Robert Burrell Donkin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package rat.report.xml;

import rat.element.IElementVisitor;
import rat.element.visitor.ElementVisitorMultiplexer;
import rat.element.visitor.IgnoreDirectoryElementVisitorProxy;
import rat.report.RatReport;
import rat.report.xml.visitor.ElementCloseVisitor;
import rat.report.xml.visitor.ElementNameReportVisitor;
import rat.report.xml.visitor.ElementOpenVisitor;
import rat.report.xml.visitor.report.RestrictedDirectoryReportVisitor;
import rat.report.xml.visitor.report.archive.ReadableArchiveVisitor;
import rat.report.xml.visitor.report.license.LicenseReportVisitor;
import rat.report.xml.writer.IXmlWriter;

/**
 * Creates reports.
 *
 */
public class XmlReportFactory {

    public static final IElementVisitor[] EMPTY_REPORTS = {};
     
    /**
     * Creates a custom report.
     * Includes the base reporting vistors
     * but excludes standard reports.
     * @param writer <code>IXmlWriter</code>, not null
     * @param extraComponents <code>IElementVisitor</code> reports, not null
     * @return <code>RatReport</code>, not null
     */
    public static final RatReport createCustomReport(final IXmlWriter writer, final IElementVisitor[] extraComponents) {
        final IElementVisitor[] reportComponents = createReportComponents(writer, extraComponents);
        return createRatReport(writer, reportComponents);
    }
    
    /**
     * Creates a custom report.
     * @param writer <code>IXmlWriter</code>, not null
     * @param attributeComponents <code>IElementVisitor</code> reports which add attributes 
     * to base element, not null
     * @param elementComponents <code>IElementVisitor</code> reports which create element content 
     * within the base element, not null
     * @return  <code>RatReport</code>, not null
     */
    public static final RatReport createCustomisedStandardReport(final IXmlWriter writer,  final IElementVisitor[] attributeComponents,  
            final IElementVisitor[] elementComponents) {
        final IElementVisitor[] extraComponents = customiseStandardReport(writer, attributeComponents, elementComponents);
        final IElementVisitor[] reportComponents = createReportComponents(writer, extraComponents);
        return createRatReport(writer, reportComponents);
    }
    
    public static final RatReport createStandardReport(final IXmlWriter writer) {
        final IElementVisitor[] extraComponents = customiseStandardReport(writer, EMPTY_REPORTS, EMPTY_REPORTS);
        final IElementVisitor[] reportComponents = createReportComponents(writer, extraComponents);
        return createRatReport(writer, reportComponents);
    }
    
    private static IElementVisitor[] customiseStandardReport(final IXmlWriter writer,  final IElementVisitor[] attributeComponents,  
        final IElementVisitor[] elementComponents) {
        final int numberOfAttributeComponents = attributeComponents.length;
        final int numberOfElementComponents = elementComponents.length;
        final int components = 3 + numberOfAttributeComponents + numberOfElementComponents;
        final IElementVisitor[] results = new IElementVisitor[components];
        results[0] = new RestrictedDirectoryReportVisitor(writer); 
        results[1] = new ReadableArchiveVisitor(writer);
        for (int i=2,j=0; j<numberOfAttributeComponents;i++, j++) {
            results[i] = attributeComponents[j];
        }
        results[numberOfAttributeComponents+2] =  new LicenseReportVisitor(writer);
        for (int i=numberOfAttributeComponents+3,j=0;j<numberOfElementComponents;i++,j++) {
            results[i] = elementComponents[j];
        }
        
        return results;
    }

    
    public static final RatReport createBaseReport(final IXmlWriter writer) {
        final IElementVisitor[] reportComponents = createReportComponents(writer, EMPTY_REPORTS);
        return createRatReport(writer, reportComponents);
    }

    private static IElementVisitor[] createReportComponents(final IXmlWriter writer, final IElementVisitor[] customComponents) {
        final int components = 3 + customComponents.length;
        final int lastComponentIndex = components - 1;
        final IElementVisitor[] reportComponents = new IElementVisitor[components];
        reportComponents[0] = new ElementOpenVisitor(writer); 
        reportComponents[1] = new ElementNameReportVisitor(writer); 
        for (int i=2,j=0; i<lastComponentIndex;i++, j++) {
            reportComponents[i] = customComponents[j];
        }
        reportComponents[lastComponentIndex] = createClosingVisitor(writer);
        return reportComponents;
    }

    private static IgnoreDirectoryElementVisitorProxy createClosingVisitor(final IXmlWriter writer) {
        return new IgnoreDirectoryElementVisitorProxy(new ElementCloseVisitor(writer));
    }

    public static final RatReport createRatReport(final IXmlWriter writer, final IElementVisitor[] reportComponents) {
        final IElementVisitor visitor = new IgnoreDirectoryElementVisitorProxy(new ElementVisitorMultiplexer(reportComponents));
        final RatReport result = new XmlReport(writer, visitor);
        return result;
    }
}
